﻿
using System;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Users
{
    public partial class addUser : SecurityPage
    {
        public void CreatedUser(object sender, EventArgs e)
        {
            activeUser.Visible = false;
            panel1.Enabled = false;
            addUserInstructions.Visible = false;
            UpdateRoleMembership(createUser.UserName);
        }

        public void CreatingUser(object sender, EventArgs e)
        {
            createUser.DisableCreatedUser = !activeUser.Checked;
        }

        public void Page_Load()
        {
            if (!IsPostBack)
            {
                PopulateCheckboxes();
            }
        }

        private void PopulateCheckboxes()
        {
            if (IsRoleManagerEnabled())
            {
                checkBoxRepeater.DataSource = CallWebAdminHelperMethod(false, "GetAllRoles", new object[] {}, null);
                checkBoxRepeater.DataBind();
                if (checkBoxRepeater.Items.Count > 0)
                {
                    selectRolesLabel.Text = (string) GetLocalResourceObject("SelectRoles");
                }
                else
                {
                    selectRolesLabel.Text = (string) GetLocalResourceObject("NoRolesDefined");
                }
            }
            else
            {
                selectRolesLabel.Text = (string) GetLocalResourceObject("RolesNotEnabled");
            }
        }

        public void SendingPasswordMail(object sender, MailMessageEventArgs e)
        {
            e.Cancel = true;
        }

        private void UpdateRoleMembership(string u)
        {
            if (String.IsNullOrEmpty(u))
            {
                return;
            }
            foreach (RepeaterItem i in checkBoxRepeater.Items)
            {
                var c = (CheckBox) i.FindControl("checkbox1");
                UpdateRoleMembership(u, c);
            }
        }

        private void UpdateRoleMembership(string u, CheckBox box)
        {
            // Array manipulation because cannot use Roles static method (need different appPath).
            string role = box.Text;

            if (box.Checked &&
                !(bool)
                 CallWebAdminHelperMethod(false, "IsUserInRole", new object[] {u, role},
                                          new[] {typeof (string), typeof (string)}))
            {
                CallWebAdminHelperMethod(false, "AddUsersToRoles", new object[] {new[] {u}, new[] {role}},
                                         new[] {typeof (string[]), typeof (string[])});
            }
            else if (!box.Checked &&
                     (bool)
                     CallWebAdminHelperMethod(false, "IsUserInRole", new object[] {u, role},
                                              new[] {typeof (string), typeof (string)}))
            {
                CallWebAdminHelperMethod(false, "RemoveUsersFromRoles", new object[] {new[] {u}, new[] {role}},
                                         new[] {typeof (string[]), typeof (string[])});
            }
        }
    }
}